import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'

const AppBaseUrl = 'http://218.109.64.68:8078'
// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  // server: {
  //   host: '0.0.0.0',
  //   port: 8080,
  //   open: true,
  //   proxy: {
  //     '/api': {
  //       target:AppBaseUrl,
  //       changeOrigin: true,
  //     },
  //     '/ai': {
  //       target: AppBaseUrl,
  //       changeOrigin: true,
  //     },
  //   },
  // },
  server: {
    host: true,
    open: true,
    port: 9526,
    proxy: {
      '/ctzhzx': {
        target: AppBaseUrl, // 本部测试环境
        changeOrigin: true,
      },
      '/arcgis': {
        target: AppBaseUrl, // CT服务器生产环境
        changeOrigin: true,
      },
      '/ai': {
        target: AppBaseUrl, // CT服务器生产环境
        changeOrigin: true,
      },
      '/mp4': {
        target: AppBaseUrl+'/mp4', // CT服务器生产环境
        changeOrigin: true,
      },
    },
    watch: {
      usePolling: true,
    },
  },
  build: {
    outDir: 'dist',
    assetsInlineLimit: 0,
    emptyOutDir: true,
    rollupOptions: {
      input: {
        main: fileURLToPath(new URL('./index.html', import.meta.url)),
      },
      output: {
        entryFileNames: 'js/[name]-[hash].js',
        chunkFileNames: 'js/[name]-[hash].js',
        assetFileNames: (assetsInfo) => {
          const imageEnd = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico']
          if (assetsInfo.names.includes('.css')) return 'css/[name]-[hash].css'
          if (imageEnd.includes(assetsInfo.names[0].split('.').pop() as string))
            return 'images/[name]-[hash].[ext]'
          else return 'assets/[name]-[hash].[ext]'
        },
      },
    },
  },
})
